/*
 *  Copyright 2006 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.apache.ibatis.ibator.api.dom.java;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import org.apache.ibatis.ibator.api.dom.OutputUtilities;
import org.apache.ibatis.ibator.internal.util.StringUtility;

/**
 * @author Jeff Butler
 */
public class TopLevelClass extends InnerClass implements CompilationUnit
{
    private Set<FullyQualifiedJavaType> importedTypes;
    
    private List<String> fileCommentLines;
    
    /**
     *  
     */
    public TopLevelClass(FullyQualifiedJavaType type)
    {
        super(type);
        importedTypes = new TreeSet<FullyQualifiedJavaType>();
        fileCommentLines = new ArrayList<String>();
    }
    
    /**
     * @return Returns the importedTypes.
     */
    public Set<FullyQualifiedJavaType> getImportedTypes()
    {
        return Collections.unmodifiableSet(importedTypes);
    }
    
    public void addImportedType(FullyQualifiedJavaType importedType)
    {
        if (importedType != null && importedType.isExplicitlyImported() && !importedType.getPackageName().equals(getType().getPackageName()))
        {
            importedTypes.add(importedType);
        }
    }
    
    public String getFormattedContent()
    {
        StringBuilder sb = new StringBuilder();
        
        for (String fileCommentLine : fileCommentLines)
        {
            sb.append(fileCommentLine);
            OutputUtilities.newLine(sb);
        }
        
        if (StringUtility.stringHasValue(getType().getPackageName()))
        {
            sb.append("package "); //$NON-NLS-1$
            sb.append(getType().getPackageName());
            sb.append(';');
            OutputUtilities.newLine(sb);
        }
        
        boolean isDAOImpl = getType().getShortName().endsWith("DAOImpl");
        StringBuilder t = new StringBuilder("BaseDAOImpl<");
        for (FullyQualifiedJavaType fqjt : importedTypes)
        {
            if (fqjt.isExplicitlyImported())
            {
                if (isDAOImpl)
                {
                    if (!fqjt.getFullyQualifiedName().contains("java.util.List"))
                    {
                        String shortName = fqjt.getShortName();
                        if (shortName.endsWith("Example"))
                        {
                            t.append(String.format("%s, %s", shortName.replace("Example", ""), shortName));
                        }
                        sb.append("import ");
                        sb.append(fqjt.getFullyQualifiedName());
                        sb.append(';');
                        OutputUtilities.newLine(sb);
                    }
                }
                else
                {
                    sb.append("import "); //$NON-NLS-1$
                    sb.append(fqjt.getFullyQualifiedName());
                    sb.append(';');
                    OutputUtilities.newLine(sb);
                }
            }
        }
        t.append(">");
        String content = super.getFormattedContent(0);
        if (isDAOImpl)
        {
            // 导入BaseDAOImpl
            sb.append("import ");
            sb.append(getType().getPackageName().replace(".dao", ".common"));
            sb.append(".BaseDAOImpl;");
            OutputUtilities.newLine(sb);
            // 导入Repository注解
            sb.append("import org.springframework.stereotype.Repository;");
            OutputUtilities.newLine(sb);
            OutputUtilities.newLine(sb);
            sb.append("@Repository");
            OutputUtilities.newLine(sb);
            content = content.replace("BaseDAOImpl", t);
        }
        sb.append(content);
        return sb.toString();
    }
    
    /*
     * (non-Javadoc)
     * 
     * @see org.apache.ibatis.ibator.internal.java.dom.CompilationUnit#isJavaInterface()
     */
    public boolean isJavaInterface()
    {
        return false;
    }
    
    public boolean isJavaEnumeration()
    {
        return false;
    }
    
    public void addFileCommentLine(String commentLine)
    {
        fileCommentLines.add(commentLine);
    }
    
    public List<String> getFileCommentLines()
    {
        return fileCommentLines;
    }
    
    public void addImportedTypes(Set<FullyQualifiedJavaType> importedTypes)
    {
        this.importedTypes.addAll(importedTypes);
    }
}
